//
//  JMCharacterViewController.m
//  VoluntaryStar
//
//  Created by ios on 15/8/27.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMCharacterViewController.h"
#include "JMBrowserViewController.h"
#import "JMCoustmButton.h"
#import "JMLoginViewController.h"


@interface JMCharacterViewController ()

@property (nonatomic,strong) UIImageView *backImageView;
@property (nonatomic,strong) NSMutableArray *itemButtonArray;


@end

@implementation JMCharacterViewController

-(UIImageView *)backImageView{
    if(!_backImageView){
        _backImageView = [[UIImageView alloc]init];
        _backImageView.backgroundColor = [UIColor clearColor];
        _backImageView.image = [UIImage imageNamed:@"secondLevel.bundle/xinggeceshi/xingge_backImage"];
        _backImageView.frame = CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT);
    }
    return _backImageView;
}
-(NSMutableArray *)itemButtonArray{
    if(!_itemButtonArray){
        _itemButtonArray = [[NSMutableArray alloc]initWithCapacity:0];
        NSArray *array = @[@"secondLevel.bundle/xinggeceshi/lizhi_image",
                           @"secondLevel.bundle/xinggeceshi/xingqu_image"];
        for(int i =0 ;i <array.count;i++){
            JMCoustmButton *btn = [[JMCoustmButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0) WithNormalImage:array[i] SelectedImage:@""];
            btn.tag = i;
            [btn addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_itemButtonArray addObject:btn];
        }
    }
    return _itemButtonArray;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
//    self.navigationItem.title = @"性格分析";
}
- (void)viewDidLoad {
    [self.view addSubview:self.backImageView];
    [self setItemButtonFrame];
    [super viewDidLoad];
}

-(void)setItemButtonFrame{
    for(int  i =0 ;i<self.itemButtonArray.count;i++){
        JMCoustmButton *btn = self.itemButtonArray[i];
        if(iphone4){
            btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.3375 * 0.75 /2.0 ,(UI_SCREEN_HEIGHT * 0.15846) + (UI_SCREEN_WITH *0.353125 * 0.75 + UI_SCREEN_HEIGHT * 0.08802) * i,
                UI_SCREEN_WITH *0.3375 *0.75,
                UI_SCREEN_WITH *0.353125 * 0.75);
        }else{
            btn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.3375/2.0 ,(UI_SCREEN_HEIGHT * 0.15846) + (UI_SCREEN_WITH *0.353125 + UI_SCREEN_HEIGHT * 0.08802) * i,
                UI_SCREEN_WITH *0.3375,
                UI_SCREEN_WITH *0.353125);
        }
        [self.view addSubview:btn];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark  item 点击 进入 网页
-(void)itemButtonClick:(UIButton *)sender{
    
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(![cf.user.state isEqualToString:@"1"]){
        [self gotoLogin];
        return;
    }

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:cf.user.userId forKey:@"user"];
    [dict setObject:cf.user.token forKey:@"token"];
    
    [JMAppNetworkClient token_WithInfo:dict complete:^(id data, NSError *error) {
        if([data[@"status"] integerValue] == 1){
            JMBrowserViewController *vc =[JMBrowserViewController new];
            if(sender.tag == 0){
                vc.url =[NSURL URLWithString:[JMAppNetworkClient inspirationalBigCoffeeUrl]];
            }else if (sender.tag == 1){
                vc.url =[NSURL URLWithString:[JMAppNetworkClient whereTheInterestUrl]];
            }
            [self.navigationController pushViewController:vc animated:NO];
                
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self gotoLogin];
        }
    }];
}
-(void)gotoLogin{
    JMLoginViewController *vc = [JMLoginViewController new];
    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
